package com.bwie.system.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.bwie.common.domain.*;
import com.bwie.common.domain.request.ShopRequest;
import com.bwie.system.mapper.ShopMapper;
import com.bwie.system.service.ShopService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @Author：作者姓名
 * @Package：com.bwie.system.service.impl
 * @Project：_yk
 * @name：ShopServiceimpl
 * @Date：2024/5/17 10:38
 */
@Service
public class ShopServiceimpl implements ShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;



    public static final String PARENT_ID="0";

    @Override
    public List<Favorable> List(ShopRequest shopRequest) {
        return shopMapper.List(shopRequest);
    }

    @Override
    public List<Gett> Find() {
        return shopMapper.Find();
    }

    @Override
    public List<Type> type() {
        return shopMapper.type();
    }

    @Override
    public List<Cate> cate() {
        return shopMapper.cate();
    }

    @Override
    public List<Shop> shop(Shop shop) {
        return shopMapper.shop(shop);
    }

    @Override
    public List<MallVo> mall() {
        List<Mall> mall = shopMapper.mall();
        List<MallVo> collect = mall.stream().filter(item -> PARENT_ID.equals(item.getParentId()))
                .map(newItem -> {
                    MallVo mallVo = new MallVo();
                    BeanUtils.copyProperties(newItem, mallVo);
                    return mallVo;
                }).map(mallVo -> {
                    mallVo.setChildrenList(parentList(mallVo, mall));
                    return mallVo;
                }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<Cat> cat() {
        return shopMapper.cat();
    }

    @Override
    public Integer ShopAdd(Shop shop) {
        Integer i = shopMapper.ShopAdd(shop);
        if(i>0){
            String id = shop.getId();
            shopMapper.Upd(id);
        }
        return i;
    }

    @Override
    public List<Shop> Shoppings() {
        return shopMapper.Shoppings();
    }

    @Override
    public Integer ShopAdds(List<Shop> shop) {
        shop.forEach(shop1 -> {
            shopMapper.ShopAdds(shop1);
        });
        return 1;
    }

    @Override
    public List<Shop> shops() {
        return shopMapper.shops();
    }

    @Override
    public Integer Del(Integer pri, String userName) {
        if(pri>=500){
            rabbitTemplate.convertAndSend("pri", JSONObject.toJSONString(userName),message -> {
                message.getMessageProperties().setMessageId(UUID.randomUUID().toString());
                return message;
            });
        }
        return shopMapper.Del();
    }

    private List<MallVo> parentList(MallVo mallVo, List<Mall> mall) {
        String catId = mallVo.getCatId();
        List<MallVo> collect = mall.stream().filter(item -> catId.equals(item.getParentId()))
                .map(newItem -> {
                    MallVo mallVo1 = new MallVo();
                    BeanUtils.copyProperties(newItem, mallVo1);
                    mallVo1.setChildrenList(parentList(mallVo1, mall));
                    return mallVo1;
                }).collect(Collectors.toList());
        return collect;
    }
}
